package com.jackeysun.log

import android.os.Process
import android.os.SystemClock
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File

/**
 *   Created by sunxunchao on 2021/12/10.
 */
class CrashHelper(val file: File) : Thread.UncaughtExceptionHandler {


    init {
        Thread.setDefaultUncaughtExceptionHandler(this) //设置该CrashHandler为系统默认的
    }

    override fun uncaughtException(t: Thread, e: Throwable) {
        GlobalScope.launch {
            val deviceInfo: String = deviceInfo()
            "${deviceInfo}\n${e.crashInfo()}".toFile(
                File(
                    file.absolutePath,
                    "crash${System.currentTimeMillis()}.log"
                )
            )
            SystemClock.sleep(2000) //延迟2秒杀进程
            Process.killProcess(Process.myPid())
            System.exit(0)
        }

    }
}